const md5 = require('blueimp-md5')
const DB = require('../tools/mysql')

module.exports = async ctx => {

  const { userId, oldPwd, newPwd } = ctx.request.body

  const [{ password }] = await DB('user').select('password').where('id', userId)

  if (md5(`tpgj${oldPwd}`) === password) {
    const result = await DB('user').where('id', userId).update('password', md5(`tpgj${newPwd}`))
    if (result === 1) {
      ctx.body = { code: 1, msg: '密码修改成功' }
    } else {
      ctx.body = { code: 0, msg: '密码修改失败，请重试' }
    }
  } else {
    ctx.body = { code: 0, msg: '旧密码错误' }
  }

}